﻿/***************************************************************************
 * 文件名：MainForm
 * 功能：
 * 说明： 引用管理主窗体
 * -------------------------------------------------------------------------
 * 创建时间：2025/7/14 8:50:43
 * 创建人：葛浩
 * 邮箱： 2094733071@qq.com
 * =========================================================================
 *
 * 修改人：   
 * 修改时间：   
 * 修改说明：   
 ***************************************************************************/
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

using Util;
using File = System.IO.File;

namespace BuiltInTools
{
	public partial class ReferenceMainForm : XtraForm
	{
		#region Propertys

		/// <summary>
		/// 解决方案文件配置
		/// </summary>
		private string _solutionFilePath;

		/// <summary>
		/// 解决方案集合
		/// </summary>
		private List<SolutionFilePath> SolutionFilePaths = new List<SolutionFilePath>();

		/// <summary>
		/// 源解决方案模型，用于搜索引用重置绑定数据
		/// </summary>
		private CSSolutionModel _SourceSolution { get; set; }

		/// <summary>
		/// 选择的项目
		/// </summary>
		private List<CSProjectModel> _Projects = new List<CSProjectModel>();

		/// <summary>
		/// 所有引用列表
		/// </summary>
		private List<AllReferenceModel> AllReferenceModels = new List<AllReferenceModel>();

		/// <summary>
		/// 程序集引用列表
		/// </summary>
		private List<AssemblyReferenceModel> AssemblyReferenceModels = new List<AssemblyReferenceModel>();

		/// <summary>
		/// 项目引用列表
		/// </summary>
		private List<ProjectReferenceModel> ProjectReferenceModels = new List<ProjectReferenceModel>();

		/// <summary>
		/// 包引用列表
		/// </summary>
		private List<PackageReferenceModel> PackageReferenceModels = new List<PackageReferenceModel>();

		#endregion

		#region Const

		/// <summary>
		/// 解决方案文件所存文件夹名称
		/// </summary>
		private const string c_Solution = "Solution";

		/// <summary>
		/// 文件名称
		/// </summary>
		private const string c_SolutionRecord = "SolutionRecord.txt";

		#endregion

		#region Win

		public ReferenceMainForm()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Load事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void MainForm_Load(object sender, EventArgs e)
		{
			InitSolutionConfiguration();
		}

		#endregion

		#region Methods

		/// <summary>
		///  初始化解决方案配置
		/// </summary>
		private void InitSolutionConfiguration()
		{
			try
			{
				//获取当前用户的Local路径
				string appDataLocalPath = FileHelperUtil.GetAppDataFolderPath();
				if (string.IsNullOrEmpty(appDataLocalPath))
				{
					return;
				}
			
				//检测解决方案配置文件所放文件夹是否存在
				string fileDir = Path.Combine(appDataLocalPath, UtilConst.AuxiliaryTools, c_Solution);
				_solutionFilePath = Path.Combine(fileDir, c_SolutionRecord);
				if (!Directory.Exists(fileDir))
				{
					Directory.CreateDirectory(fileDir);
					//创建文件
					using (FileStream fs = new FileStream(_solutionFilePath, FileMode.Create))
					{

					}
				}
				else
				{
					if (!File.Exists(_solutionFilePath))
					{
						return;
					}

					//读取文件
					List<string> allLines = File.ReadAllLines(_solutionFilePath).ToList();
					if (allLines?.Count <= 0)
					{
						return;
					}

					//遍历往解决方案集合中添加数据
					foreach (string path in allLines)
					{
						//检测解决方案文件是否存在，若文件不存在跳过
						if (string.IsNullOrEmpty(path) || !File.Exists(path))
						{
							continue;
						}

						string fileName = Path.GetFileName(path);
						SolutionFilePaths.Add(new SolutionFilePath() { SolutionPath = path, SolutionName = fileName });
					}

					//绑定数据到解决方案列表
					listBox.SelectedValueChanged -= listBox_SelectedValueChanged;

					listBox.DataSource = SolutionFilePaths;

					listBox.SelectedValueChanged += listBox_SelectedValueChanged;
				}

			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 加载解决方案配置
		/// </summary>
		private void LoadSolutionConfiguration()
		{
			if (listBox.SelectedItem == null && listBox.Items.Count > 0)
			{
				listBox.SelectedValueChanged -= listBox_SelectedValueChanged;

				listBox.SelectedIndex = 0;

				listBox.SelectedValueChanged += listBox_SelectedValueChanged;
			}

			var selectedItem = listBox.SelectedItem as SolutionFilePath;
			if (selectedItem == null)
			{
				return;
			}
			
			//加载解决方案的引用
			LoadSolution(selectedItem, true);
		}

		/// <summary>
		/// 加载解决方案
		/// </summary>
		private void LoadSolution(SolutionFilePath solution, bool isFirstLoad = false)
		{
			try
			{
				string filePath = solution?.SolutionPath;
				if (string.IsNullOrEmpty(filePath))
				{
					return;
				}

				if (!File.Exists(filePath))
				{
					MessageBox.Show("当前选择的解决方案文件不存在或已被删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				labSolutionName.Text = solution.SolutionName;

				//加载配置
				LoadingSolutionReference(filePath);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 带加载信息提示的
		/// </summary>
		/// <param name="filePath"></param>
		private void LoadingSolutionReference(string filePath, bool isSearch = false)
		{
			//加载配置
			this.BeginInvoke( new Action(() =>
			{
				LoadSolutionRefrence(filePath, isSearch);
			}));
		}

		/// <summary>
		/// 加载解决方案文件的所有引用
		/// </summary>
		/// <param name="solutionFilePath"></param>
		/// <param name="isSearch"></param>
		private void LoadSolutionRefrence(string solutionFilePath, bool isSearch = false)
		{
			try
			{
				#region 数据获取

				AllReferenceModels.Clear();
				bsDataAllReference.DataSource = null;
				allReferenceGrid.MainView.RefreshData();

				AssemblyReferenceModels.Clear();
				bsDataAssembly.DataSource = null;
				assemblyGrid.MainView.RefreshData();

				ProjectReferenceModels.Clear();
				bsDataProject.DataSource = null;
				projectGrid.MainView.RefreshData();

				PackageReferenceModels.Clear();
				bsDataPackage.DataSource = null;
				packageGrid.MainView.RefreshData();

				_Projects.Clear();

				//源解决方案所有引用
				_SourceSolution = CSSolutionModel.GetSolution(solutionFilePath, new List<string>());

				if (_SourceSolution == null)
				{
					return;
				}

				#endregion

				#region 选择的项目

				if (_SourceSolution.Projects?.Count == 0)
				{
					_Projects.AddRange(_SourceSolution.Projects);
				}

				#endregion

				#region 所有引用

				if (_SourceSolution.AllReferenceList?.Count > 0)
				{
					//若存在搜索，替换引用后仍然保持程序集引用搜索
					if (isSearch)
					{
						var list = GetAllReferenceModels();
						if (list?.Count > 0)
						{
							AllReferenceModels.AddRange(list);
						}
					}
					else
					{
						AllReferenceModels.AddRange(_SourceSolution.AllReferenceList);
					}

					bsDataAllReference.DataSource = AllReferenceModels;
					allReferenceGrid.MainView.RefreshData();
				}

				#endregion

				#region 程序集的引用

				if (_SourceSolution.AllAssemblyReferenceList?.Count > 0)
				{
					//若存在搜索，替换引用后仍然保持程序集引用搜索
					if (isSearch)
					{

						var list = GetAssemblyReferenceModels();
						if (list?.Count > 0)
						{
							AssemblyReferenceModels.AddRange(list);
						}
					}
					else
					{
						AssemblyReferenceModels.AddRange(_SourceSolution.AllAssemblyReferenceList);
					}

					bsDataAssembly.DataSource = AssemblyReferenceModels;
					assemblyGrid.MainView.RefreshData();
				}

				#endregion

				#region 项目引用

				if (_SourceSolution.AllProjectReferenceList?.Count > 0)
				{
					//若存在搜索，替换引用后仍然保持项目引用搜索
					if (isSearch)
					{
						var list = GetProjectReferenceModels();
						if (list?.Count > 0)
						{
							ProjectReferenceModels.AddRange(list);
						}
					}
					else
					{
						ProjectReferenceModels.AddRange(_SourceSolution.AllProjectReferenceList);
					}

					bsDataProject.DataSource = ProjectReferenceModels;
					projectGrid.MainView.RefreshData();
				}

				#endregion

				#region 包引用

				if (_SourceSolution.AllPackageReferenceList?.Count > 0)
				{
					//若存在搜索，替换引用后仍然保持包引用搜索
					if (isSearch)
					{
						var list = GetPackageReferenceModels();
						if (list?.Count > 0)
						{
							PackageReferenceModels.AddRange(list);
						}
					}
					else
					{
						PackageReferenceModels.AddRange(_SourceSolution.AllPackageReferenceList);
					}

					bsDataPackage.DataSource = PackageReferenceModels;
					packageGrid.MainView.RefreshData();

				}

				#endregion
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置所有引用搜索方法
		/// </summary>
		private void AllRefernenceResetSerach(bool isClearAll = false)
		{
			try
			{
				txtAllProjectName.Text = string.Empty;
				txtAllReferenceName.Text = string.Empty;
				AllReferenceModels.Clear();

				if (!isClearAll && _SourceSolution?.AllReferenceList?.Count > 0)
				{
					AllReferenceModels.AddRange(_SourceSolution.AllReferenceList);
				}

				bsDataAllReference.DataSource = AllReferenceModels;
				allReferenceGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置程序集搜索方法
		/// </summary>
		private void AssemblyRefernenceResetSerach(bool isClearAll= false)
		{
			try
			{
				txtProjectNameofAssembly.Text = string.Empty;
				txtReferenceNameofAssembly.Text = string.Empty;
				txtAssemblyRelativePath.Text = string.Empty;
				AssemblyReferenceModels.Clear();

				if (!isClearAll && _SourceSolution?.AllAssemblyReferenceList?.Count > 0)
				{
					AssemblyReferenceModels.AddRange(_SourceSolution.AllAssemblyReferenceList);
				}

				bsDataAssembly.DataSource = AssemblyReferenceModels;
				assemblyGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置项目引用搜索方法
		/// </summary>
		private void ProjectRefernenceResetSerach(bool isClearAll = false)
		{
			try
			{
				txtProjectNameofProject.Text = string.Empty;
				txtReferenceNameofProject.Text = string.Empty;
				txtRelativePathofProject.Text = string.Empty;

				ProjectReferenceModels.Clear();

				if (!isClearAll && _SourceSolution?.AllProjectReferenceList?.Count > 0)
				{
					ProjectReferenceModels.AddRange(_SourceSolution.AllProjectReferenceList);
				}

				bsDataProject.DataSource = ProjectReferenceModels;
				projectGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置包引用搜索方法
		/// </summary>
		private void PackageRefernenceResetSerach(bool isClearAll = false)
		{
			try
			{
				txtProjectNameofPackage.Text = string.Empty;
				txtPackageName.Text = string.Empty;
				txtVersionType.Text = string.Empty;

				PackageReferenceModels.Clear();

				if (!isClearAll && _SourceSolution?.AllPackageReferenceList?.Count > 0)
				{
					PackageReferenceModels.AddRange(_SourceSolution.AllPackageReferenceList);
				}

				bsDataPackage.DataSource = PackageReferenceModels;
				packageGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 获取所有引用列表
		/// </summary>
		/// <returns></returns>
		private List<AllReferenceModel> GetAllReferenceModels()
		{
			var list = _SourceSolution?.AllReferenceList?
												   .Where(dataItem =>
												   {
													   if (!string.IsNullOrWhiteSpace(txtAllProjectName.Text) && (string.IsNullOrWhiteSpace(dataItem.ProjectName)
													   || !dataItem.ProjectName.Contains(txtAllProjectName.Text)))
													   {
														   return false;
													   }

													   if (!string.IsNullOrWhiteSpace(txtAllReferenceName.Text) && (string.IsNullOrWhiteSpace(dataItem.ReferenceName)
													   || !dataItem.ReferenceName.Contains(txtAllReferenceName.Text)))
													   {
														   return false;
													   }
													   return true;
												   }).OrderBy(p => p.ProjectName).ThenBy(p => p.ReferenceName).ToList();

			return list;
		}

		/// <summary>
		/// 获取程序集引用模型列表
		/// </summary>
		/// <returns></returns>
		private List<AssemblyReferenceModel> GetAssemblyReferenceModels()
		{
			var list = _SourceSolution?.AllAssemblyReferenceList?
										   .Where(dataItem =>
										   {
											   if (!string.IsNullOrWhiteSpace(txtProjectNameofAssembly.Text) && (string.IsNullOrWhiteSpace(dataItem.ProjectName)
											   || !dataItem.ProjectName.Contains(txtProjectNameofAssembly.Text)))
											   {
												   return false;
											   }
											   if (!string.IsNullOrWhiteSpace(txtReferenceNameofAssembly.Text) && (string.IsNullOrWhiteSpace(dataItem.AssemblyReferenceName)
											   || !dataItem.AssemblyReferenceName.Contains(txtReferenceNameofAssembly.Text)))
											   {
												   return false;
											   }
											   if (!string.IsNullOrWhiteSpace(txtAssemblyRelativePath.Text) && (string.IsNullOrWhiteSpace(dataItem.RelativePath)
											   || !dataItem.RelativePath.Contains(txtAssemblyRelativePath.Text)))
											   {
												   return false;
											   }
											   return true;
										   }).OrderBy(p => p.ProjectName).ThenBy(p => p.AssemblyReferenceName).ToList();
			return list;
		}

		/// <summary>
		/// 获取项目引用模型列表
		/// </summary>
		/// <returns></returns>
		private List<ProjectReferenceModel> GetProjectReferenceModels()
		{
			var list = _SourceSolution?.AllProjectReferenceList?
										   .Where(dataItem =>
										   {
											   if (!string.IsNullOrWhiteSpace(txtProjectNameofProject.Text) && (string.IsNullOrWhiteSpace(dataItem.ProjectName)
											   || !dataItem.ProjectName.Contains(txtProjectNameofProject.Text)))
											   {
												   return false;
											   }
											   if (!string.IsNullOrWhiteSpace(txtReferenceNameofProject.Text) && (string.IsNullOrWhiteSpace(dataItem.ProjectReferneceName)
											   || !dataItem.ProjectReferneceName.Contains(txtReferenceNameofProject.Text)))
											   {
												   return false;
											   }
											   if (!string.IsNullOrWhiteSpace(txtRelativePathofProject.Text) && (string.IsNullOrWhiteSpace(dataItem.RelativePath)
											   || !dataItem.RelativePath.Contains(txtRelativePathofProject.Text)))
											   {
												   return false;
											   }
											   return true;
										   }).OrderBy(p => p.ProjectName).ThenBy(p => p.ProjectReferneceName).ToList();
			return list;
		}

		/// <summary>
		/// 获取包引用模型列表
		/// </summary>
		/// <returns></returns>
		private List<PackageReferenceModel> GetPackageReferenceModels()
		{
			var list = _SourceSolution?.AllPackageReferenceList?
										   .Where(dataItem =>
										   {
											   if (!string.IsNullOrWhiteSpace(txtProjectNameofPackage.Text) && (string.IsNullOrWhiteSpace(dataItem.ProjectName)
											   || !dataItem.ProjectName.Contains(txtProjectNameofPackage.Text)))
											   {
												   return false;
											   }
											   if (!string.IsNullOrWhiteSpace(txtPackageName.Text) && (string.IsNullOrWhiteSpace(dataItem.PackageReferenceName)
											   || !dataItem.PackageReferenceName.Contains(txtPackageName.Text)))
											   {
												   return false;
											   }
											   if (!string.IsNullOrWhiteSpace(txtVersionType.Text) && (string.IsNullOrWhiteSpace(dataItem.VersionType)
											   || !dataItem.VersionType.Contains(txtVersionType.Text)))
											   {
												   return false;
											   }
											   return true;
										   }).OrderBy(p => p.ProjectName).ThenBy(p => p.PackageReferenceName).ToList();
			return list;
		}

		/// <summary>
		/// 清空当前解决方案的所有控件值
		/// </summary>
		private void ClearTabItemsControlValue()
		{
			try
			{
				labSolutionName.Text = string.Empty;

				#region 所有引用

				AllRefernenceResetSerach(true);

				#endregion

				#region 程序集的引用

				///清空搜索框
				AssemblyRefernenceResetSerach(true);

				#endregion

				#region 项目引用

				///清空搜索框
				ProjectRefernenceResetSerach(true);

				#endregion

				#region 包引用

				///清空搜索框
				PackageRefernenceResetSerach(true);

				#endregion
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 程序集引用替换其他引用方法
		/// </summary>
		private void AssemblyReplaceReference(string refType)
		{
			try
			{
				var gridView = assemblyGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<AssemblyReferenceModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as AssemblyReferenceModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}

				ReplaceReference(selectRows, null, null, refType);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 项目引用替换其他引用方法
		/// </summary>
		private void ProjectReplaceReference(string refType)
		{
			try
			{
				var gridView = projectGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<ProjectReferenceModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as ProjectReferenceModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}
				ReplaceReference(null, selectRows, null, refType);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 包引用替换其他引用方法
		/// </summary>
		private void PackageReplaceReference(string refType)
		{
			try
			{
				var gridView = packageGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<PackageReferenceModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as PackageReferenceModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}
				ReplaceReference(null, null, selectRows, refType);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region Common Methods

		/// <summary>
		/// 添加引用
		/// </summary>
		/// <param name="projects"></param>
		/// <param name="refType"></param>
		private void AddRef(List<CSProjectModel> projects, string refType)
		{

			if (listBox.SelectedItem == null)
			{
				return;
			}

			var selectedItem = listBox.SelectedItem as SolutionFilePath;
			if (selectedItem == null)
			{
				return;
			}

			List<CSProjectModel> projectList = projects.GroupBy(p => new { p.ProjectName, p.RelativePath, p.FileName }).
											Select(p => CSProjectModel.CreateDefault(p.Key.ProjectName, p.Key.RelativePath, p.Key.FileName)).ToList();

			FMAddReference form = new FMAddReference(selectedItem.SolutionPath, projectList, refType, string.Empty);

			// 显示模态窗口
			DialogResult = form.ShowDialog();
			if (DialogResult != DialogResult.OK)
			{
				return;
			}

			//刷新数据
			LoadingSolutionReference(selectedItem.SolutionPath, true);
		}

		/// <summary>
		/// 替换引用
		/// </summary>
		private void ReplaceReference(List<AssemblyReferenceModel> assemblyReferences, List<ProjectReferenceModel> projectReferences,
									  List<PackageReferenceModel> packageReferences, string targetRefType)
		{
			try
			{
				bool isExistAssembyRefData = assemblyReferences?.Count > 0;
				bool isExistProjectRefData = projectReferences?.Count > 0;
				bool isExistPackageRefData = packageReferences?.Count > 0;

				if (!isExistAssembyRefData && !isExistProjectRefData && !isExistPackageRefData)
				{
					MessageBox.Show("请选择要替换的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				if (string.IsNullOrEmpty(targetRefType))
				{
					MessageBox.Show("替换的目标引用类型不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				FMInputBox form = new FMInputBox(targetRefType);
				DialogResult dialogResult = form.ShowDialog();
				if (dialogResult != DialogResult.OK)
				{
					return;
				}

				string inputValue = form.InputValue;
				if (string.IsNullOrEmpty(inputValue))
				{
					return;
				}

				string message = string.Empty;
				if (isExistAssembyRefData)
				{
					assemblyReferences.ForEach(dataItem =>
					{
						ReferenceEditor.Delete(dataItem.ProjectFileName, ReferenceTypeConst.Ref, dataItem.AssemblyReferenceName);
						message = ReferenceEditor.InsertReference(dataItem.ProjectFileName, targetRefType, inputValue);
					});
				}
				else if (isExistProjectRefData)
				{
					projectReferences.ForEach(dataItem =>
					{
						ReferenceEditor.Delete(dataItem.ProjectFileName, ReferenceTypeConst.ProjRef, dataItem.ProjectReferneceName);
						message = ReferenceEditor.InsertReference(dataItem.ProjectFileName, targetRefType, inputValue);

					});
				}
				else if (isExistPackageRefData)
				{
					packageReferences.ForEach(dataItem =>
					{
						ReferenceEditor.Delete(dataItem.ProjectFileName, ReferenceTypeConst.PkgRef, dataItem.PackageReferenceName);
						message = ReferenceEditor.InsertReference(dataItem.ProjectFileName, targetRefType, inputValue);
					});
				}

				if (string.IsNullOrEmpty(message))
				{
					MessageBox.Show(message);
					return;
				}

				MessageBox.Show("替换成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

				var selectedItem = listBox.SelectedItem as SolutionFilePath;
				if(selectedItem == null)
				{
					return;
				}

				string solutionPath = selectedItem.SolutionPath;
				if (string.IsNullOrEmpty(solutionPath) && !File.Exists(solutionPath))
				{
					MessageBox.Show($"解决方案{solutionPath}不存在或已被删除！");
					return;
				}

				//加载配置
				LoadingSolutionReference(solutionPath, true);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region Event

		#region 解决方案事件

		/// <summary>
		/// 加载解决方案
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnLoadSolution_Click(object sender, EventArgs e)
		{
			LoadSolutionConfiguration();
		}

		/// <summary>
		/// 添加解决方案
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddSolution_Click(object sender, EventArgs e)
		{
			try
			{
				var dialog = new OpenFileDialog();
				dialog.DefaultExt = ".sln";
				dialog.Multiselect = false;
				dialog.Filter = "解决方案文件(.sln)|*.sln";

				if (dialog.ShowDialog() != DialogResult.OK)
				{
					return;
				}

				string filePath = dialog.FileName;

				if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
				{
					return;
				}

				if (!File.Exists(_solutionFilePath))
				{
					string diretory = Path.GetDirectoryName(_solutionFilePath);
					if (!Directory.Exists(diretory))
					{
						Directory.CreateDirectory(diretory);
					}

					//创建文件
					using (FileStream fs = new FileStream(_solutionFilePath, FileMode.Create))
					{
						
					}
				}

				SolutionFilePath solutionFilePath = new SolutionFilePath()
				{
					SolutionPath = filePath,
					SolutionName = Path.GetFileName(filePath)
				};

				SolutionFilePaths.Add(solutionFilePath);

				listBox.DataSource = SolutionFilePaths;
				listBox.Refresh();

				//写入文件中
				using (FileStream fs = new FileStream(_solutionFilePath, FileMode.Create, FileAccess.ReadWrite))
				{
					using (StreamWriter writer = new StreamWriter(fs))
					{
						writer.Write("");
						foreach (var item in SolutionFilePaths)
						{
							writer.WriteLine(item.SolutionPath);
						}
						writer.Flush();
						writer.Close();
						fs.Close();
					}
				}
			 
				listBox.SelectedValueChanged -= listBox_SelectedValueChanged;

				listBox.SetSelected(SolutionFilePaths.IndexOf(solutionFilePath),true);

				listBox.SelectedValueChanged += listBox_SelectedValueChanged;

				labSolutionName.Text = solutionFilePath.SolutionName;

				//加载配置
				LoadingSolutionReference(filePath);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		///  删除解决方案
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteSolution_Click(object sender, EventArgs e)
		{
			try
			{
				var selectedItem = listBox.SelectedItem as SolutionFilePath;
				if(selectedItem == null || string.IsNullOrEmpty(selectedItem.SolutionPath))
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				if (!SolutionFilePaths.Any(p => p.SolutionPath == selectedItem.SolutionPath))
				{
					return;
				}

				SolutionFilePaths.Remove(selectedItem);
				listBox.DataSource = SolutionFilePaths;
				listBox.Refresh();

				//读取头文件配置路径
				using (StreamWriter writer = new StreamWriter(_solutionFilePath))
				{
					writer.Write("");
					foreach (var path in SolutionFilePaths)
					{
						writer.WriteLine(path.SolutionPath);
					}
				}

				//若删除的解决方案文件为当前选择的解决方案，清空各TabItem控件的数据
				if (labSolutionName.Text.Equals(selectedItem.SolutionName))
				{
					ClearTabItemsControlValue();
				}

				MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 刷新解决方案
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnRefreshSolution_Click(object sender, EventArgs e)
		{
			try
			{
				var solution = listBox.SelectedItem as SolutionFilePath;

				if (solution == null || string.IsNullOrEmpty(solution.SolutionPath))
				{
					MessageBox.Show("请选择要刷新的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				//检测文件是否存在，若不存在则删除解决方案配置文件中的数据
				if (!File.Exists(solution.SolutionPath))
				{
					SolutionFilePaths.Remove(solution);
					listBox.DataSource = SolutionFilePaths;
					listBox.Refresh();

					//读取头文件配置路径
					using (StreamWriter writer = new StreamWriter(_solutionFilePath))
					{
						writer.Write("");
						foreach (var path in SolutionFilePaths)
						{
							writer.WriteLine(path.SolutionPath);
						}
					}

					//若当前不存在解决方案的文件，则清空控件值
					if (SolutionFilePaths.Count < 0)
					{
						labSolutionName.Text = string.Empty;
						ClearTabItemsControlValue();
						return;
					}

					//默认加载第一个文件的引用
					var selectItem = SolutionFilePaths.FirstOrDefault();

					listBox.SelectedValueChanged -= listBox_SelectedValueChanged;

					listBox.SetSelected(SolutionFilePaths.IndexOf(selectItem), true);

					listBox.SelectedValueChanged += listBox_SelectedValueChanged;

					labSolutionName.Text = selectItem.SolutionName;

					//加载
					LoadingSolutionReference(selectItem.SolutionPath);
				}
				else
				{
					labSolutionName.Text = solution.SolutionName;
					LoadingSolutionReference(solution.SolutionPath);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		///  解决方案列表选中值改变事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void listBox_SelectedValueChanged(object sender, EventArgs e)
		{
			var selectedItem = listBox.SelectedItem as SolutionFilePath;
			if(selectedItem == null)
			{
				return;
			}
			LoadSolution(selectedItem);
		}

		#endregion

		#region 引用事件

		/// <summary>
		/// 搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAllSearch_Click(object sender, EventArgs e)
		{
			try
			{
				AllReferenceModels.Clear();

				var list = GetAllReferenceModels();
				if (list?.Count > 0)
				{
					AllReferenceModels.AddRange(list);
				}

				//刷新数据
				bsDataAllReference.DataSource = AllReferenceModels;
				allReferenceGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAllResetSearch_Click(object sender, EventArgs e)
		{
			AllRefernenceResetSerach();
		}

		/// <summary>
		/// 查看引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDisplayReference_Click(object sender, EventArgs e)
		{
			try
			{
				var gridView = allReferenceGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var model = gridView.GetFocusedRow() as AllReferenceModel;
				if(model == null)
				{
					return;
				}

				if (string.IsNullOrEmpty(model.ReferenceType) || string.IsNullOrEmpty(model.ProjectName)
					|| string.IsNullOrEmpty(model.ReferenceName))
				{
					return;
				}

				switch (model.ReferenceType)
				{
					case ReferenceTypeConst.Reference:
						{
							xtraTabControl1.SelectedTabPageIndex = 1;
							int index = AssemblyReferenceModels.FindIndex(p => p.ProjectName == model.ProjectName && p.AssemblyReferenceName == model.ReferenceName);
							if (index == -1)
							{
								return;
							}

							var gridRow = assemblyGrid.MainView as GridView;
							if(gridRow != null)
							{
								gridRow.FocusedRowHandle = index;
								gridRow.SelectRow(index);
								gridRow.MakeRowVisible(index);
							}
							break;
						}
					case ReferenceTypeConst.ProjectReference:
						{
							xtraTabControl1.SelectedTabPageIndex = 2;
							var index = ProjectReferenceModels.FindIndex(p => p.ProjectName == model.ProjectName && p.ProjectReferneceName == model.ReferenceName);
							if (index == -1)
							{
								return;
							}

							var gridRow = projectGrid.MainView as GridView;
							if (gridRow != null)
							{
								gridRow.FocusedRowHandle = index;
								gridRow.SelectRow(index);
								gridRow.MakeRowVisible(index);
							}
							break;
						}
					case ReferenceTypeConst.PackageReference:
						{
							xtraTabControl1.SelectedTabPageIndex = 3;
							int index = PackageReferenceModels.FindIndex(p => p.ProjectName == model.ProjectName && p.PackageReferenceName == model.ReferenceName);
							if (index == -1)
							{
								return;
							}

							var gridRow = packageGrid.MainView as GridView;
							if (gridRow != null)
							{
								gridRow.FocusedRowHandle = index;
								gridRow.SelectRow(index);
								gridRow.MakeRowVisible(index);
							}
							break;
						}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 引用GridView右键菜单显示事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void gridView2_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
		{
			// 检查是否是右键点击
			if (e.HitInfo.InRowCell)
			{
				// 显示您的 ContextMenuStrip
				contextMenuStrip1.Show(Cursor.Position);
			}
		}

		#endregion

		#region 程序集引用事件

		/// <summary>
		/// 搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAssemblySearch_Click(object sender, EventArgs e)
		{
			try
			{
				AssemblyReferenceModels.Clear();

				var list = GetAssemblyReferenceModels();
				if (list?.Count > 0)
				{
					AssemblyReferenceModels.AddRange(list);
				}

				//刷新数据
				bsDataAssembly.DataSource = AssemblyReferenceModels;
				assemblyGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAssemblyResetSearch_Click(object sender, EventArgs e)
		{
			AssemblyRefernenceResetSerach();
		}

		/// <summary>
		/// 添加程序集引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddReferenceofAssembly_Click(object sender, EventArgs e)
		{
			try
			{
				AddRef(AssemblyReferenceModels.Select(p => CSProjectModel.CreateDefault(p.ProjectName, string.Empty, p.ProjectFileName)).ToList(), ReferenceTypeConst.Ref);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		///  删除程序集引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteReferenceofAssembly_Click(object sender, EventArgs e)
		{
			try
			{
				var gridView = assemblyGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<AssemblyReferenceModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as AssemblyReferenceModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}

				//循环删除数据
				selectRows.ForEach(dataItem =>
				{
					ReferenceEditor.Delete(dataItem.ProjectFileName, ReferenceTypeConst.Ref, dataItem.AssemblyReferenceName);
					AssemblyReferenceModels.Remove(dataItem);

					_Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AssemblyReferenceModels.RemoveAll(p => p.AssemblyReferenceName == dataItem.AssemblyReferenceName);

					_SourceSolution.AllAssemblyReferenceList.Remove(dataItem);
					_SourceSolution.Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AssemblyReferenceModels.RemoveAll(p => p.AssemblyReferenceName == dataItem.AssemblyReferenceName);

					//删除所有引用中的数据
					var data = AllReferenceModels.Find(p => p.ProjectName == dataItem.ProjectName && p.ReferenceName == dataItem.AssemblyReferenceName);
					if (data != null)
					{
						AllReferenceModels.Remove(data);
						_SourceSolution.AllReferenceList.Remove(data);
					}
					_Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AllReferenceModels.RemoveAll(p => p.ReferenceName == dataItem.AssemblyReferenceName);
					_SourceSolution.Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AllReferenceModels.RemoveAll(p => p.ReferenceName == dataItem.AssemblyReferenceName);
				});

				MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

				//刷新数据
				bsDataAssembly.DataSource = AssemblyReferenceModels;
				assemblyGrid.MainView.RefreshData();

				bsDataAllReference.DataSource = AssemblyReferenceModels;
				allReferenceGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 替换为项目引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReplaceProjectRefofAssembly_Click(object sender, EventArgs e)
		{
			AssemblyReplaceReference(ReferenceTypeConst.ProjRef);
		}

		/// <summary>
		/// 替换为包引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReplacePackageRefofAssembly_Click(object sender, EventArgs e)
		{
			AssemblyReplaceReference(ReferenceTypeConst.PkgRef);
		}

		#endregion

		#region 项目引用事件

		/// <summary>
		/// 搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnProjectSearch_Click(object sender, EventArgs e)
		{
			try
			{
				ProjectReferenceModels.Clear();

				var list = GetProjectReferenceModels();
				if (list?.Count > 0)
				{
					ProjectReferenceModels.AddRange(list);
				}

				//刷新数据
				bsDataProject.DataSource = ProjectReferenceModels;
				projectGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void btnProjectResetSearch_Click(object sender, EventArgs e)
		{
			ProjectRefernenceResetSerach();
		}

		/// <summary>
		/// 添加项目引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void btnAddReferenceofProject_Click(object sender, EventArgs e)
		{
			try
			{
				AddRef(this.ProjectReferenceModels.Select(p => CSProjectModel.CreateDefault(p.ProjectName, string.Empty, p.ProjectFileName)).ToList(), ReferenceTypeConst.ProjRef);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 删除项目引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteReferenceofProject_Click(object sender, EventArgs e)
		{
			try
			{
				var gridView = projectGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<ProjectReferenceModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as ProjectReferenceModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}

				//循环删除数据
				selectRows.ForEach(dataItem =>
				{
					ReferenceEditor.Delete(dataItem.ProjectFileName, ReferenceTypeConst.ProjRef, dataItem.ProjectReferneceName);
					ProjectReferenceModels.Remove(dataItem);
					_Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.ProjectReferenceModels.RemoveAll(p => p.ProjectReferneceName == dataItem.ProjectReferneceName);
					_SourceSolution.AllProjectReferenceList.Remove(dataItem);
					_SourceSolution.Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.ProjectReferenceModels.RemoveAll(p => p.ProjectReferneceName == dataItem.ProjectReferneceName);

					//删除所有引用中的数据
					var data = AllReferenceModels.Find(p => p.ProjectName == dataItem.ProjectName && p.ReferenceName == dataItem.ProjectReferneceName);
					if (data != null)
					{
						AllReferenceModels.Remove(data);

						_SourceSolution.AllReferenceList.Remove(data);
					}
					_Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AllReferenceModels.RemoveAll(p => p.ReferenceName == dataItem.ProjectReferneceName);
					_SourceSolution.Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AllReferenceModels.RemoveAll(p => p.ReferenceName == dataItem.ProjectReferneceName);
				});

				MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

				//刷新数据
				bsDataProject.DataSource = ProjectReferenceModels;
				projectGrid.MainView.RefreshData();

				bsDataAllReference.DataSource = AllReferenceModels;
				allReferenceGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 替换为程序集引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReplaceAssemblyRefofProject_Click(object sender, EventArgs e)
		{
			ProjectReplaceReference(ReferenceTypeConst.Ref);
		}

		/// <summary>
		/// 替换为包引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReplacePackageRefofProject_Click(object sender, EventArgs e)
		{
			ProjectReplaceReference(ReferenceTypeConst.PkgRef);
		}

		#endregion

		#region 包引用事件

		/// <summary>
		/// 搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnPackageSearch_Click(object sender, EventArgs e)
		{
			try
			{
				PackageReferenceModels.Clear();

				var list = GetPackageReferenceModels();
				if (list?.Count > 0)
				{
					PackageReferenceModels.AddRange(list);
				}

				//刷新数据
				bsDataPackage.DataSource = PackageReferenceModels;
				packageGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 重置搜索
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnPackageResetSearch_Click(object sender, EventArgs e)
		{
			PackageRefernenceResetSerach();
		}

		/// <summary>
		/// 添加包引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnAddReferenceofPackage_Click(object sender, EventArgs e)
		{
			try
			{
				AddRef(PackageReferenceModels.Select(p => CSProjectModel.CreateDefault(p.ProjectName, string.Empty, p.ProjectFileName)).ToList(), ReferenceTypeConst.PkgRef);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 删除包引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnDeleteReferenceofPackage_Click(object sender, EventArgs e)
		{
			try
			{
				var gridView = packageGrid.MainView as GridView;
				if (gridView == null)
				{
					return;
				}

				var selectRowIndexs = gridView.GetSelectedRows();
				if (selectRowIndexs?.Length < 0)
				{
					MessageBox.Show("请选择要删除的数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
					return;
				}

				var selectRows = new List<PackageReferenceModel>();
				for (int i = 0; i < selectRowIndexs?.Length; i++)
				{
					var dataItem = gridView.GetRow(selectRowIndexs[i]) as PackageReferenceModel;
					if (dataItem != null)
					{
						selectRows.Add(dataItem);
					}
				}

				if (selectRows.Count <= 0)
				{
					return;
				}

				//循环删除数据
				selectRows.ForEach(dataItem =>
				{
					ReferenceEditor.Delete(dataItem.ProjectFileName, ReferenceTypeConst.PkgRef, dataItem.PackageReferenceName);
					PackageReferenceModels.Remove(dataItem);
					_Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.PackageReferenceModels.RemoveAll(p => p.PackageReferenceName == dataItem.PackageReferenceName);
					_SourceSolution.AllPackageReferenceList.Remove(dataItem);
					_SourceSolution.Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.PackageReferenceModels.RemoveAll(p => p.PackageReferenceName == dataItem.PackageReferenceName);

					//删除所有引用中的数据
					var data = AllReferenceModels.Find(p => p.ProjectName == dataItem.ProjectName && p.ReferenceName == dataItem.PackageReferenceName);
					if (data != null)
					{
						AllReferenceModels.Remove(data);
						_SourceSolution.AllReferenceList.Remove(data);
					}
					_Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AllReferenceModels.RemoveAll(p => p.ReferenceName == dataItem.PackageReferenceName);
					_SourceSolution.Projects.FirstOrDefault(p => p.ProjectName == dataItem.ProjectName)?.AllReferenceModels.RemoveAll(p => p.ReferenceName == dataItem.PackageReferenceName);
				});

				MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

				//刷新数据
				bsDataPackage.DataSource = PackageReferenceModels;
				packageGrid.MainView.RefreshData();

				bsDataAllReference.DataSource = AllReferenceModels;
				allReferenceGrid.MainView.RefreshData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		/// <summary>
		/// 替换为程序集引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReplaceAssemblyRefofPackage_Click(object sender, EventArgs e)
		{
			PackageReplaceReference(ReferenceTypeConst.Ref);
		}

		/// <summary>
		/// 替换为项目引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void btnReplaceProjectRefofPackage_Click(object sender, EventArgs e)
		{
			PackageReplaceReference(ReferenceTypeConst.ProjRef);
		}

		/// <summary>
		/// 替换为包引用
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnReplacePackageRefofPackage_Click(object sender, EventArgs e)
		{
			PackageReplaceReference(ReferenceTypeConst.PkgRef);
		}

		#endregion

		#endregion

		#region 复制单元格的值

		private void gridView2_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control && e.KeyCode == Keys.C)
			{
				var focusedCell = gridView2.FocusedColumn;
				if (focusedCell != null)
				{
					Clipboard.SetText(gridView2.GetFocusedValue()?.ToString());
					e.Handled = true; // 阻止默认复制行为
				}
			}
		}

		private void gridView3_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control && e.KeyCode == Keys.C)
			{
				var focusedCell = gridView3.FocusedColumn;
				if (focusedCell != null)
				{
					Clipboard.SetText(gridView3.GetFocusedValue()?.ToString());
					e.Handled = true; // 阻止默认复制行为
				}
			}
		}

		private void gridView4_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control && e.KeyCode == Keys.C)
			{
				var focusedCell = gridView4.FocusedColumn;
				if (focusedCell != null)
				{
					Clipboard.SetText(gridView4.GetFocusedValue()?.ToString());
					e.Handled = true; // 阻止默认复制行为
				}
			}
		}

		private void gridView1_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control && e.KeyCode == Keys.C)
			{
				var focusedCell = gridView1.FocusedColumn;
				if (focusedCell != null)
				{
					Clipboard.SetText(gridView1.GetFocusedValue()?.ToString());
					e.Handled = true; // 阻止默认复制行为
				}
			}
		}
	
		#endregion
	}
}
